<?php
/** 单品（比如汉堡和冷饮） */
interface Item
{
    /** @return string */
    public function name();
    /** @return Packing */
    public function packing();
    /** @return float */
    public function price();
}
/** 食品包装 */
interface Packing
{
    /** @return self */
    public function pack();
}

class Wrapper implements Packing
{
    public function pack()
    {
        return "Wrapper";
    }
}
class Bottle implements Packing
{
    public function pack()
    {
        return "Bottle";
    }
}

abstract class Burger implements Item
{
    public function packing()
    {
        return new Wrapper;
    }
}
abstract class ColdDrink implements Item
{
    public function packing()
    {
        return new Bottle;
    }
}

class VegBurger extends Burger
{
    public function price()
    {
        return 25.0;
    }
    public function name()
    {
        return "Veg Burger";
    }
}
class ChickenBurger extends Burger
{
    public function price()
    {
        return 50.5;
    }
    public function name()
    {
        return "Chicken Burger";
    }
}

class Coke extends ColdDrink
{
    public function price()
    {
        return 30.0;
    }
    public function name()
    {
        return "Coke";
    }
}
class Pepsi extends ColdDrink
{
    public function price()
    {
        return 35.0;
    }
    public function name()
    {
        return "Pepsi";
    }
}

class Meal
{
    /** @var Item[] $items */
    protected $items = [];//new Item;
    public function addItem($item)
    {
        $this->items[] = $item;
    }
    public function getCost()
    {
        $cost=0;
        foreach($this->items as $item){
            $cost += $item->price();
        }
        return $cost;
    }
    public function showItems()
    {
        foreach($this->items as $item){
            echo "Item : " . $item->name();
            echo ", Packing : " . $item->packing()->pack();
            echo ", Price : " . $item->price() ."\r\n";
        }
    }
}

class MealBuilder
{
    public function prepareVegMeal(){
        $meal=new Meal();
        $meal->addItem(new VegBurger);
        $meal->addItem(new Coke);
        return $meal;
     }   
   
     public function prepareNonVegMeal(){
        $meal=new Meal();
        $meal->addItem(new ChickenBurger);
        $meal->addItem(new Pepsi);
        return $meal;
     }
}

header('Content-Type: text/plain');
$builder=new MealBuilder;
$veg_meal=$builder->prepareVegMeal();
echo "Veg Meal\r\n";
$veg_meal->showItems();
echo "Total Cost: " . $veg_meal->getCost() ."\r\n";

$non_veg_meal=$builder->prepareNonVegMeal();
echo "Non-Veg Meal\r\n";
$non_veg_meal->showItems();
echo "Total Cost: " . $non_veg_meal->getCost() ."\r\n";

